
using Microsoft.VisualStudio.Modeling.Diagrams;
using System.Drawing;
using Microsoft.VisualStudio.Modeling;
using DslFactory.Utilities.DteCodeModel;
using EnvDTE;
namespace Dslfactory.wcfLanguage
{
 public partial class InterfaceShape
 {
  /// <summary>
  /// Changes the delegate used to obtain the icon of the Method in an Interface shape
  /// </summary>
  /// <param name="melType"></param>
  /// <returns></returns>
  protected override CompartmentMapping[] GetCompartmentMappings(System.Type melType)
  {
   CompartmentMapping[] mappings = base.GetCompartmentMappings(melType);
   ElementListCompartmentMapping operationMapping = mappings[0] as ElementListCompartmentMapping;
   operationMapping.ImageGetter = GetElementImage;
   return mappings;
  }

  /// <summary>
  /// Decides what the icon of the method will be in the interface shape
  /// </summary>
  /// <param name="mel"></param>
  /// <returns></returns>
  Image GetElementImage(ModelElement mel)
  {
   if (mel is OperationContract)
    return Properties.Resources.ServiceContract_Method;
   else
    return Properties.Resources.VSObject_Method ;
  }

  /// <summary>
  /// Represented interface
  /// </summary>
  public Interface Interface
  {
   get
   {
    return ModelElement as Interface;
   }
  }

  /// <summary>
  /// When double-clicking on a shape, we get to the type
  /// </summary>
  /// <param name="e"></param>
  public override void OnDoubleClick(DiagramPointEventArgs e)
  {
   KnownCodeTypes knownCodeTypes = KnownCodeTypes.FromStore(Store);
   if (knownCodeTypes == null)
    return;
    CodeType codeType = knownCodeTypes.GetFullNamedType(Interface.FullName);
    if (codeType == null)
     return;

   foreach (ModelElement represented in e.HitDiagramItem.RepresentedElements)
   {
    ModelElement element = (represented is ShapeElement) ? (represented as ShapeElement).ModelElement : represented;
    if (element is Interface)
     IncrementalGenerator.TryToShow(codeType as CodeElement);
    else if (element is Method)
    {
     CodeFunction method = IncrementalGenerator.FindMethod(codeType as CodeInterface, (element as Method).Signature);
     if (method != null)
      IncrementalGenerator.TryToShow(method as CodeElement);
     
    }
   }
  }
 }
}
